<?php

namespace App;

use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
use DB;


class User extends Authenticatable
{
    use Notifiable;
	protected $table = 'users';
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'email', 'password',
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];

    public function getUsersList(){
	    $users = DB::table('users')
		    ->leftjoin('admin_role_users', 'users.id', '=', 'admin_role_users.user_id')
		    ->leftjoin('admin_roles', 'admin_roles.id', '=', 'admin_role_users.role_id')
		    ->select('users.*', 'admin_roles.role_name as roles_name')
		    ->get()
		    ->toArray();
	    if(!$users){
		    return array();
	    }
	    return $users;
    }

	public static  function get_user_one($id){
		$users = DB::table('users')
			->leftjoin('admin_role_users', 'users.id', '=', 'admin_role_users.user_id')
			->select('users.*','admin_role_users.role_id')
			->where('users.id',$id)
			->get()
//			->toSql();
			->toArray();
		if(!$users){
			return array();
		}
		return $users[0];
	}

	// 根据id获取用户名
	public static function getNameById($id){
		$userName = '';
		$userName = User::where('id',$id)->pluck('name');
		if($userName){
			$userName = $userName[0];
		}
		return $userName;
	}
	// 根据账号获取id值
	public static function getIdByName($name){
		$userId = '';
		$userId = User::where('name',trim($name))->pluck('id');
		if($userId){
			$userId = $userId[0];
		}
		return $userId;
	}

	// 根据id获取用户的角色
	public static function getUserIdByRole($uid){
		$roleArr = DB::table('users')
			->leftjoin('admin_role_users', 'users.id', '=', 'admin_role_users.user_id')
			->select('admin_role_users.role_id')
			->where('users.id',$uid)
			->get()
//			->toSql();
			->toArray();
		if(!$roleArr){
			return array();
		}
		foreach ($roleArr as $value){
			return $value->role_id;
		}

	}

}
